USE QPTreasureDB
GO

IF EXISTS (SELECT * FROM DBO.SYSOBJECTS WHERE ID = OBJECT_ID(N'[dbo].[ShareSong]')
AND OBJECTPROPERTY(ID, N'IsProcedure') = 1)
DROP PROCEDURE [dbo].[ShareSong]
GO

CREATE PROC ShareSong
	@dwUserID	INT,
	@strErrorDescribe NVARCHAR(127) OUTPUT
AS

SET NOCOUNT ON

BEGIN
	--判断用户是否存在
	DECLARE @UserID INT
	SELECT @UserID=UserID FROM GameScoreInfo(NOLOCK) WHERE UserID=@dwUserID
	IF @UserID IS NULL
	BEGIN
		SET @strErrorDescribe='用户'+Convert(NVARCHAR(30),@dwUserID)+'不存在'
		RETURN 1
	END
	
	--判断用户赠送表是否存在
	if not exists (select * from dbo.sysobjects where id = object_id(N'dbo.AccountsShareDonate') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
	begin
		CREATE TABLE dbo.AccountsShareDonate (UserID INT NOT NULL,DonateDate DATE NOT NULL)
	end
	
	--判断是否当天能否赠送
	DECLARE @DonateDate DATE
	SELECT @DonateDate=DonateDate FROM dbo.AccountsShareDonate WHERE UserID=@UserID
	IF @DonateDate IS NOT NULL
	BEGIN
		IF DATEDIFF(dd,@DonateDate,GetDate()) <= 0
		BEGIN
			SET @strErrorDescribe='今天的赠送次数已用完'
			RETURN 2
		END
	END
	
	--获取赠送数量
	DECLARE @addScore INT
	SELECT @addScore=StatusValue FROM QPAccountsDB.dbo.SystemStatusInfo WHERE StatusName='ShareDonateGold'
	IF @addScore IS NULL
	BEGIN
		INSERT INTO QPAccountsDB.dbo.SystemStatusInfo(StatusName,StatusValue,StatusString) VALUES('ShareDonateGold',5,'微信分享赠送金币')
	END
	DECLARE @addInsureScore INT
	SELECT @addInsureScore=StatusValue FROM QPAccountsDB.dbo.SystemStatusInfo WHERE StatusName='ShareDonateFangKa'
	IF @addInsureScore IS NULL
	BEGIN
		INSERT INTO QPAccountsDB.dbo.SystemStatusInfo(StatusName,StatusValue,StatusString) VALUES('ShareDonateFangKa',1,'微信分享赠送房卡')
	END

	--获取原有数量
	DECLARE	@oldScore INT
	DECLARE	@oldInsureScore INT
	SELECT @oldInsureScore=InsureScore,@oldScore=Score FROM GameScoreInfo WHERE UserID=@dwUserID
	
	--更新银行表
	UPDATE GameScoreInfo SET InsureScore=@oldInsureScore+@addInsureScore,Score=@oldScore+@addScore WHERE UserID=@dwUserID
	
	--更新赠送记录表
	IF EXISTS(SELECT * FROM dbo.AccountsShareDonate WHERE UserID=@UserID)
		BEGIN
			UPDATE dbo.AccountsShareDonate SET DonateDate=GetDate() WHERE UserID=@UserID
		END
	ELSE
		BEGIN
			INSERT INTO dbo.AccountsShareDonate(UserID,DonateDate) VALUES(@UserID,GetDate())
		END
	
	SET @strErrorDescribe='赠送钻石成功'
END

RETURN 0

GO

